/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Controller.CoreServlet;

import Model.BeanUltility.ResultsetMapper;
import Model.DataUltility.DatabaseConnector;
import Model.ObjectBean.ViewBean.PersonalInfo_FrDatabase;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author Tran Nguyen Anh Tu
 */
public class ChangeInfoServlet extends HttpServlet {

    String errormessage;

    boolean validate(String name, String address, String phonenumber, int day, int month, int year) {
        boolean flag = true;
        if (name.length() == 0) {
            errormessage += "Tên không được bỏ trống<br/>";
            flag = false;
        }
        if (address.length() == 0) {
            errormessage += "Địa chỉ không được bỏ trống<br/>";
            flag = false;
        }
        try {
            Double.parseDouble(phonenumber);
        } catch (Exception e) {
            errormessage += "Số điện thoại không hợp lệ<br/>";
            flag = false;
        }
        if (year % 4 != 0 && month == 2 && day > 28) {
            errormessage += "Ngày sinh không hơp lệ<br/>";
            flag = false;
        }
        if ((month == 4 || month == 6 || month == 9 || month == 11) && day > 30) {
            errormessage += "Ngày sinh không hơp lệ<br/>";
            flag = false;
        }
        return flag;
    }

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("UTF-8");
        resp.setCharacterEncoding("UTF-8");
        resp.setContentType("text/html; charset=UTF-8");

        errormessage="";
        PrintWriter out = resp.getWriter();      
        String userID = ((PersonalInfo_FrDatabase) req.getSession().getAttribute("perInfo")).getId();
        PersonalInfo_FrDatabase newUserInfo = new PersonalInfo_FrDatabase();
        String updateQuerry = "update personal_info set ";
        String selectQuerry = "select * from personal_info where id='"+userID+"'";
        //EDITABLE CODE
        int day = Integer.parseInt(req.getParameter("day"));
        int month = Integer.parseInt(req.getParameter("month"));
        int year = Integer.parseInt(req.getParameter("year"));

        String dateofbirth = year + "-" + month + "-" + day;
        String midAndlastname = req.getParameter("midAndlastname");
        String address = req.getParameter("address");
        String phonenumber = req.getParameter("phonenumber");
        String city = req.getParameter("city");
        String gender = req.getParameter("gender");

        if (validate(midAndlastname, address, phonenumber, day, month, year)) {
            try {
                updateQuerry += "birthday='" + dateofbirth + "' ,lastnae='" + midAndlastname + "' "
                        + ",address='" + address + "' ,phonenumber='" + phonenumber + "' "
                        + ",city_id='" + city + "' ,sex='" + gender + "' where id='" + userID + "'";
                DatabaseConnector.executeQueryString(updateQuerry);
                ResultsetMapper.mapperResultsettoObject(DatabaseConnector.getResultSet(selectQuerry),
                        PersonalInfo_FrDatabase.class, newUserInfo);
                req.getSession().removeAttribute("perInfo");
                req.getSession().setAttribute("perInfo", newUserInfo);
                System.out.println(updateQuerry);
                errormessage += "Cập nhật thành công";
            }
            catch(Exception e){System.out.println(e);}
        }
         else {
            errormessage += "Cập nhật không thành công";
        }

        req.setAttribute("message", errormessage);
        RequestDispatcher dispatcher = req.getRequestDispatcher("/WEB-INF/MainJSP/changeuserinfo.jsp");
        dispatcher.forward(req, resp);

    }
}
